﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.Web;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.Model.Enum;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.Common;

namespace HZ.DrugTrade.Web.Enterprise.RelationQuery
{

    public partial class CompanySelect : BasePage
    {
        public CompanySelect()
        { }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (string.IsNullOrEmpty(Request.QueryString["areaid"]))
                {
                    SetBind();
                }
                else
                {
                    SetBind(Request.QueryString["areaid"]);
                }
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Request.QueryString["areaid"]))
            {
                SetBind();
            }
            else
            {
                SetBind(Request.QueryString["areaid"]);
            }
        }

        private void SetBind()
        {

            try
            {

                HZ.DrugTrade.Model.Query.Company company = new HZ.DrugTrade.Model.Query.Company();
                company.CompanyName = this.txtProduceCompany_PS.Text.Trim();
                //company.CompanyType = EnumCompanyType.医药物流企业;
                company.Append = string.Format("CompanyID in (select CompanyPS_Id from TB_PSSelectArea where checkStatus=1)");//add by zdc
                AspNetPager1.PageSize = 10;
                int totalRecord = 0;
                IBLL_Company iPurchase = Factory.BLL_Company();
                IList<Company> lstcompany = iPurchase.GetCompanyByWhere(company, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalRecord);
                AspNetPager1.RecordCount = totalRecord;
                gvwGoodsInfo.DataSource = lstcompany;
                gvwGoodsInfo.DataBind();
            }
            catch (Exception ex)
            {
                PageError(ex.Message, "");
            }

        }
        private void SetBind(string areaID)
        {
            try
            {
                HZ.DrugTrade.Model.Query.Company company = new HZ.DrugTrade.Model.Query.Company();
                company.CompanyName = this.txtProduceCompany_PS.Text.Trim();
                //company.CompanyType = EnumCompanyType.配送企业;
//                company.Append = string.Format(@" (CompanyType = 1 or CompanyType = 2 ) and CompanyID in (SELECT CompanyID_PS
//  FROM TB_SelectArea where AreaIDs like '%{0}%') ", areaID);
                //if (areaID!="150000")//如果没有选择特定的地区 by zdc
                //{
                    company.Append = string.Format(@"(CompanyType <>0 ) and CompanyID in (SELECT distinct CompanyPS_Id
  FROM TB_PSSelectArea where AreaID like '%{0}%' and checkStatus=1 and checkByZbzx=1)", areaID);//修改 by zdc
//                }
//                else
//                {
//                    company.Append = string.Format(@"(CompanyType <>0 ) and CompanyID in (SELECT distinct CompanyPS_Id
// FROM TB_PSSelectArea where  checkStatus=1)");//修改 by zdc
//                }

                AspNetPager1.PageSize = 10;
                int totalRecord = 0;
                IBLL_Company iPurchase = Factory.BLL_Company();
                IList<Company> lstcompany = iPurchase.GetCompanyByWhere(company, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalRecord);
                AspNetPager1.RecordCount = totalRecord;
                gvwGoodsInfo.DataSource = lstcompany;
                gvwGoodsInfo.DataBind();
            }
            catch (Exception ex)
            {
                PageError(ex.Message, "");
            }

        }
        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Request.QueryString["areaid"]))
            {
                SetBind();
            }
            else
            {
                SetBind(Request.QueryString["areaid"]);
            }
        }


    }
}
